﻿using TomatoClock.Models;
using TomatoClock.ViewModels;

namespace TomatoClock.Views
{
    public partial class AchievementsControl : UserControl
    {
        private FlowLayoutPanel flowLayoutPanel;
        private List<Achievement> CurAchievements;
        private List<Achievement> AllAchievements;
        private List<Achievement> MissAchievements;
        private MainViewModel viewModel;
        public void setViewModel(MainViewModel v)
        {
            viewModel = v;
            viewModel.dataService.AchievementUnlocked += OnAchievementUnlocked;
        }
        public int achievementCount { get; private set; }

        public void setCurAchievements(List<Achievement> a)
        {
            CurAchievements = a;
            if (CurAchievements == null || CurAchievements.Count == 0)
            {
                //MessageBox.Show("0");
                achievementCount = 0;
            }
            else
            {
                achievementCount = CurAchievements.Count;
                //MessageBox.Show(achievementCount.ToString());
            }
        }

        public void setMissAchievements(List<Achievement> a)
        {
            MissAchievements = a;
        }
        public void setAllAchievements(List<Achievement> a)
        {
            AllAchievements = a;
        }

        public AchievementsControl()
        {
            InitializeComponent();
            InitializeFlowLayoutPanel(); // 需要确保 flowLayoutPanel 被初始化
        }
        private void OnAchievementUnlocked()
        {
            // 在事件处理程序中更新成就列表
          CurAchievements = viewModel.dataService.GetAchievementsForUser(viewModel.CurrentUser.UserId); // 获取当前解锁的成就
          MissAchievements = viewModel.dataService.GetMissingAchievements(viewModel.CurrentUser.UserId); // 获取未解锁的成就

            DisplayAchievements();
        }
        public AchievementsControl(List<Achievement> a)
        {
            this.CurAchievements = a;
            InitializeComponent();
            InitializeFlowLayoutPanel();
            DisplayAchievements();
        }

        private void InitializeFlowLayoutPanel()
        {
            flowLayoutPanel = new FlowLayoutPanel
            {
                Dock = DockStyle.Fill,
                AutoScroll = true,
                FlowDirection = FlowDirection.TopDown,
                WrapContents = false,
                Padding = new Padding(10) //控制内部距离
            };
            this.Controls.Add(flowLayoutPanel);
        }

        public void DisplayAchievements()
        {
            flowLayoutPanel.Controls.Clear(); // 清空现有控件

            foreach (var achievement in CurAchievements)
            {
                var achievementPanel = CreateAchievementPanel(achievement);
                flowLayoutPanel.Controls.Add(achievementPanel);
                //MessageBox.Show("生成一个成就");//调试
            }

            foreach (var achievement in MissAchievements)
            {
                var achievementPanel = CreateLockAchievementPanel(achievement);
                flowLayoutPanel.Controls.Add(achievementPanel);
            }

            flowLayoutPanel.Refresh(); // 刷新控件
        }

        private Panel CreateAchievementPanel(Achievement achievement)
        {
            var panel = new Panel
            {
                Width = flowLayoutPanel.ClientSize.Width - 40, // Adjust to the FlowLayoutPanel width
                Height = 72,
                Margin = new Padding(5),
                BorderStyle = BorderStyle.FixedSingle
            };

            var tableLayoutPanel = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 3, // 3列：图片、描述、解锁时间
            };
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 64)); // 图片列宽度
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); // 描述列宽度
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); // 解锁时间列宽度

            var pictureBox = new PictureBox
            {
                Size = new Size(64, 64),
                SizeMode = PictureBoxSizeMode.StretchImage
            };

            if (!string.IsNullOrEmpty(achievement.ImagePath))
            {
                string path = Path.Combine(Application.StartupPath, "Resources/Achievements", achievement.ImagePath);
                if (File.Exists(path))
                {
                    pictureBox.Image = Image.FromFile(path);
                }
                else
                {
                    // 处理图片路径不存在的情况
                    pictureBox.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Resources/Achievements", "Lock.png"));//以锁代替
                }
            }
            else
            {
                // 处理图片路径为空的情况
                pictureBox.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Resources/Achievements", "Lock.png"));//以锁代替
            }

            var descriptionLabel = new Label
            {
                Text = $"{achievement.Name}\n\n{achievement.Description}",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleLeft,
            };

            var completeTimeLabel = new Label
            {
                //Text = achievement.CompleteTime.ToString(), // 解锁时间
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleRight,
            };

            // 将控件添加到 TableLayoutPanel 中
            tableLayoutPanel.Controls.Add(pictureBox, 0, 0); // 图片
            tableLayoutPanel.Controls.Add(descriptionLabel, 1, 0); // 描述
            tableLayoutPanel.Controls.Add(completeTimeLabel, 2, 0); // 解锁时间

            // 将 TableLayoutPanel 添加到 Panel 中
            panel.Controls.Add(tableLayoutPanel);

            return panel;
        }

        private Panel CreateLockAchievementPanel(Achievement achievement)
        {
            var panel = new Panel
            {
                Width = flowLayoutPanel.ClientSize.Width - 40, // Adjust to the FlowLayoutPanel width
                Height = 72,
                Margin = new Padding(5),
                BorderStyle = BorderStyle.FixedSingle
            };

            var tableLayoutPanel = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 3, // 3列：图片、描述、解锁时间
            };
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 64)); // 图片列宽度
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); // 描述列宽度
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); // 解锁时间列宽度

            var pictureBox = new PictureBox
            {
                Size = new Size(64, 64),
                SizeMode = PictureBoxSizeMode.StretchImage
            };

            pictureBox.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Resources/Achievements", "Lock.png"));//以锁代替

            var descriptionLabel = new Label
            {
                Text = $"{achievement.Name}\n\n{achievement.Description}",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleLeft,
            };

            var completeTimeLabel = new Label
            {
                Text = "未解锁",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleRight,
            };

            // 将控件添加到 TableLayoutPanel 中
            tableLayoutPanel.Controls.Add(pictureBox, 0, 0); // 图片
            tableLayoutPanel.Controls.Add(descriptionLabel, 1, 0); // 描述
            tableLayoutPanel.Controls.Add(completeTimeLabel, 2, 0); // 解锁时间

            // 将 TableLayoutPanel 添加到 Panel 中
            panel.Controls.Add(tableLayoutPanel);

            return panel;
        }
    }

}
